package info.touret.winecellar.client;

import info.touret.winecellar.bottle.client.BottleListUI;
import info.touret.winecellar.bottle.client.BottleRequestFactory;
import info.touret.winecellar.bottle.client.BottleUI;
import info.touret.winecellar.bottle.client.TastingListUI;
import info.touret.winecellar.bottle.client.TastingRequestFactory;
import info.touret.winecellar.producer.client.ProducerListUI;
import info.touret.winecellar.producer.client.ProducerRequestFactory;
import info.touret.winecellar.producer.client.ProducerUI;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.Style.Overflow;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.resources.client.CssResource.NotStrict;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiFactory;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.SplitLayoutPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.web.bindery.event.shared.SimpleEventBus;

/**
 * @todo gerer une factory pour tous les panels.
 * @author touret-a
 *
 */
public class Winecellar implements EntryPoint {

	interface Binder extends UiBinder<DockLayoutPanel, Winecellar> {
	}

	interface GlobalResources extends ClientBundle {
		@NotStrict
		@Source("global.css")
		CssResource css();
	}

	private static final Binder binder = GWT.create(Binder.class);

	@UiField
	TopPanel topPanel;
	// @UiField MailList mailList;
	@UiField
	ProducerListUI producerList;
	// @UiField
	// MailDetail mailDetail;
	@UiField
	Shortcuts shortcuts;
	@UiField
	SplitLayoutPanel splitLayoutPanel;

	ProducerUI producerAdd;

	BottleListUI bottleList;
	BottleUI bottleAdd;

	/**
	 * This method constructs the application user interface by instantiating
	 * controls and hooking up event handler.
	 */
	public void onModuleLoad() {

		GWT.<GlobalResources> create(GlobalResources.class).css()
				.ensureInjected();

		DockLayoutPanel outer = binder.createAndBindUi(this);

		Window.enableScrolling(false);
		Window.setMargin("0px");

		// Special-case stuff to make topPanel overhang a bit.
		Element topElem = outer.getWidgetContainerElement(topPanel);
		topElem.getStyle().setZIndex(2);
		topElem.getStyle().setOverflow(Overflow.VISIBLE);
		initializeHistory();

		RootLayoutPanel root = RootLayoutPanel.get();
		root.add(outer);
	}

	/**
	 * Initialise l historique de navigation
	 */
	private void initializeHistory() {
		History.addValueChangeHandler(new ValueChangeHandler<String>() {
			public void onValueChange(ValueChangeEvent<String> event) {
				String historyToken = event.getValue();
				if (historyToken.equals("producerlist")) {
					if (getCurrentCenterWidget() != null) {
						splitLayoutPanel.remove(getCurrentCenterWidget());
					}
					splitLayoutPanel.add(producerList);
					setCurrentCenterWidget(producerList);
				} else if (historyToken.equals("addproducer")) {
					if (getCurrentCenterWidget() != null) {
						splitLayoutPanel.remove(getCurrentCenterWidget());
					}
					if (producerAdd == null) {
						producerAdd = makeProducerUI();
					}
					splitLayoutPanel.add(producerAdd);
					setCurrentCenterWidget(producerAdd);
				} else if (historyToken.equals("addbottle")) {
					splitLayoutPanel.remove(getCurrentCenterWidget());
					splitLayoutPanel.add(getBottleAdd());
					setCurrentCenterWidget(getBottleAdd());
				} else if (historyToken.equals("bottleslist")) {
					splitLayoutPanel.remove(getCurrentCenterWidget());
					splitLayoutPanel.add(getBottleList());
					setCurrentCenterWidget(getBottleList());
				} else if (historyToken.equals("tastinglist")) {
					splitLayoutPanel.remove(getCurrentCenterWidget());
					splitLayoutPanel.add(getTastinglistUI());
					setCurrentCenterWidget(getTastinglistUI());
				}
			}
		});

	}

	/** Used by MyUiBinder to instantiate CricketScores */
	@UiFactory
	ProducerListUI makeProducerlist() { // method name is insignificant
		ProducerRequestFactory requestFactory = GWT
				.create(ProducerRequestFactory.class);
		requestFactory.initialize(new SimpleEventBus());
		return new ProducerListUI(requestFactory);
	}

	private BottleListUI makeBottlelist() { // method name is insignificant
		BottleRequestFactory requestFactory = GWT
				.create(BottleRequestFactory.class);
		ProducerRequestFactory producerrequestFactory = GWT
				.create(ProducerRequestFactory.class);
		requestFactory.initialize(new SimpleEventBus());
		producerrequestFactory.initialize(new SimpleEventBus());
		return new BottleListUI(requestFactory, producerrequestFactory);
	}

	private ProducerUI makeProducerUI() {
		ProducerRequestFactory producerrequestFactory = GWT
				.create(ProducerRequestFactory.class);
		producerrequestFactory.initialize(new SimpleEventBus());
		return new ProducerUI(producerrequestFactory);

	}

	private BottleUI makeBottleUI() {
		BottleRequestFactory bottlerequestfactory = GWT
				.create(BottleRequestFactory.class);
		bottlerequestfactory.initialize(new SimpleEventBus());
		ProducerRequestFactory requestFactory = GWT
				.create(ProducerRequestFactory.class);
		requestFactory.initialize(new SimpleEventBus());
		return new BottleUI(bottlerequestfactory, requestFactory);

	}

	private Widget currentCenterWidget;

	public Widget getCurrentCenterWidget() {
		if (currentCenterWidget == null) {
			setCurrentCenterWidget(producerList);
		}
		return currentCenterWidget;
	}

	public void setCurrentCenterWidget(Widget currentCenterWidget) {
		this.currentCenterWidget = currentCenterWidget;
	}

	public BottleListUI getBottleList() {
		if (bottleList == null) {
			setBottleList(makeBottlelist());
		}
		return bottleList;
	}

	public void setBottleList(BottleListUI bottleList) {
		this.bottleList = bottleList;
	}

	public BottleUI getBottleAdd() {
		if (bottleAdd == null) {
			setBottleAdd(makeBottleUI());
		}
		return bottleAdd;
	}

	public void setBottleAdd(BottleUI bottleAdd) {
		this.bottleAdd = bottleAdd;
	}

	private TastingListUI tastingListUI;

	/**
	 * @return la liste initialisee
	 */
	private TastingListUI makeTastingListUI() {
		TastingRequestFactory requestFactory = GWT
				.create(TastingRequestFactory.class);
		requestFactory.initialize(new SimpleEventBus());
		return new TastingListUI(requestFactory);

	}

	/**
	 * @return
	 */
	public TastingListUI getTastinglistUI() {
		if (tastingListUI == null) {
			setTastingListUI(makeTastingListUI());
		}
		return tastingListUI;
	}

	public void setTastingListUI(TastingListUI tastingUI) {
		this.tastingListUI = tastingUI;
	}

}
